package cn.itcast.feign.service;


import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.mapper.OrderMapper;
import cn.itcast.feign.pojo.Order;
import cn.itcast.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.利用Feign发起HTTP请求，查询用户信息
        User user = userClient.findById(order.getUserId());
        // 3.封装user到order
        order.setUser(user);
        // 4.返回
        return order;
    }
    /*public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order feign = orderMapper.findById(orderId);
        // 2.利用RestTemplate发起http请求
        // 2.1 url路径
        String url = "http://userservice/user/" + feign.getUserId();
        // 2.2 发送http请求，实现远程调用
        User user = restTemplate.getForObject(url, User.class);
        // 3.封装user到order
        feign.setUser(user);
        // 4.返回
        return feign;
    }*/
}
